package terminal.model;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

/**
 * Created on Dec 26, 2012
 * @author P. Podwapinski <p.podwapinski@gmail.com>
 */
public class TerminalModel {
    
    /**
     * Constructor
     */
    public TerminalModel()
    {
        m_document = new PlainDocument();
        m_lastNLChar = 0;        
    }
    
    
    public void addStringData( String data )
    {
        tryInsertingTextToDocument( m_lastNLChar, data );
        m_lastNLChar = m_document.getLength();
    }
    
    
    /**
     * Exposes the internal document instance used to hold the incoming data
     * @return Document interface
     */
    public Document getDocument()
    {
        return m_document;
    }
    
    
    private boolean tryInsertingTextToDocument( int offset, String string )
    {
        boolean result = true;
        
        try
        {
            m_document.insertString( offset, string, null );
        }
        catch( BadLocationException e )
        {
            result = false;
        }
        
        return result;
    }    
    
    
    /**
     * PlainDocument used to hold output
     */
    private PlainDocument m_document;
    
    /**
     * Last NextLine character position in the Document
     */
    private int m_lastNLChar;
    
    
    private static final char CHAR_NUL = '\0';
    
    
    /**
     * Escape character definition
     */
    private static final char CHAR_ESC = 0x1B;
    
    /**
     * Carriage Return character definition
     */
    private static final char CHAR_CR = '\r';
    
    
    /**
     * Next Line character definition
     */
    private static final char CHAR_NL = '\n';
    
}
